package com.testing.login;

import com.alibaba.fastjson.JSONObject;
import com.testing.module.UserInfo;
import com.testing.mysql.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Classname ${NAME}
 * @Description 类型说明
 * @Date 2021/9/11 22:44
 * @Created by 特斯汀Roy
 */
@WebServlet(name = "GetUser")
public class GetUser extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        UserInfo userResult=new UserInfo();
        if (session.getAttribute("username")!=null){
            List<Map<String, String>> userinfo = MysqlUtils.queryAll("select * from userinfo where username='" + session.getAttribute("username") + "'");
            Map<String, String> userinfoMap = userinfo.get(0);
            userResult.setUserId(userinfoMap.get("id"));
            userResult.setNickname(userinfoMap.get("nickname"));
            userResult.setDescription(userinfoMap.get("describe"));
            userResult.setStatusCode("0");
            userResult.setMsg("用户信息查询成功。");
        }else{
            userResult.setStatusCode("-1");
            userResult.setMsg("您还没有登录，不能进行查询");
        }
        String s = JSONObject.toJSONString(userResult);
        System.out.println(s);
        response.getWriter().append(s);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("不支持通过get方法来查询用户信息");
    }
}
